﻿using UnityEngine;
using System.Collections;

public class MouseLook : MonoBehaviour {

	/// MouseLook rotates the transform based on the mouse delta.
	/// Minimum and Maximum values can be used to constrain the possible rotation
	
	/// To make an FPS style character:
	/// - Create a capsule.
	/// - Add the MouseLook script to the capsule.
	///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
	/// - Add FPSInputController script to the capsule
	///   -> A CharacterMotor and a CharacterController component will be automatically added.
	
	/// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
	/// - Add a MouseLook script to the camera.
	///   -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
	/// 
	//[RequireComponent(CharacterMotor)]
	[AddComponentMenu("Camera-Control/Mouse Look")]
	 #region NESTED
	public enum RotationAxes{ 
		MouseXAndY, 
		MouseX, 
		MouseY
	}
	 #endregion // NESTED
	//enum RotationAxes {MouseXAndY, MouseX, MouseY}
	public RotationAxes axes = RotationAxes.MouseXAndY;
	
	private float minRotX = -30.0f;
	private float maxRotX = 45.0f;
	public bool change = true;
	private float camSens = 0.25F; 
	private GameObject settingObj;
	private Vector3 lastMouse;
	private bool isDrag = false;
	
	private float curTime = 0;//Inertia
	private Vector3 lastPV  = Vector3.zero;
	float smoothTime = 2;
	public static bool MovementDirty = false;
	void Awake(){
		settingObj = GameObject.Find("SettingGUI").gameObject;
	}
	void Update ()
	{		
		var guiScript = (SettingScript)settingObj.GetComponent(typeof(SettingScript)); 
		camSens = guiScript.camSenScroll;
		if(change)
			maxRotX = 315;
		else
			maxRotX = 340;
	
		if (Input.touchCount>=2)
		{ 
		
		}		
		else
		{
				if (Input.GetMouseButton(0) && isDrag )
				{
					if ( Input.touchCount > 0)
			    		lastMouse = new Vector3 (Input.GetTouch(0).position.x - lastMouse.x,Input.GetTouch(0).position.y - lastMouse.y,0); 					
					else
						lastMouse = Input.mousePosition - lastMouse ;
					
				    if (lastMouse.magnitude > 5)
				    {
				    	lastMouse = new Vector3(-lastMouse.y * camSens, lastMouse.x * camSens, 0 );
						curTime = 0;											
						lastPV = lastMouse;
						if (axes == RotationAxes.MouseXAndY)
						{
							transform.eulerAngles = new Vector3(transform.eulerAngles.x + lastMouse.x , transform.eulerAngles.y + lastMouse.y, 0);
						}
						else if (axes == RotationAxes.MouseX)
						{
							transform.eulerAngles = new Vector3(transform.eulerAngles.x , transform.eulerAngles.y + lastMouse.y, 0);
						}
						else
						{
							transform.eulerAngles = new Vector3(transform.eulerAngles.x  + lastMouse.x, transform.eulerAngles.y , 0);
						}		
				    }
					//=======Duc ++=======
					else if(curTime < smoothTime && lastPV != Vector3.zero)
					{						
						lastPV = Vector3.Lerp(lastPV, Vector3.zero, curTime/smoothTime); 
						curTime += Time.smoothDeltaTime * 6;
					}
					//====================
					if ( Input.touchCount > 0)
			    		lastMouse = Input.GetTouch(0).position;
					else
						lastMouse = Input.mousePosition;
					
			    }
			    else
					if (Input.GetMouseButton(0))
					{
						isDrag = true;
						if(Input.GetMouseButtonUp(0))
						{	
							MovementDirty = true;
						}
						if ( Input.touchCount > 0)
				    		lastMouse = Input.GetTouch(0).position; 					
						else
							lastMouse = Input.mousePosition;
					}
					else
					{
						isDrag = false;
						MovementDirty = false;
					}
				//=======Duc ++=======
			if(!isDrag && curTime < smoothTime && lastPV != Vector3.zero)
			{	
				lastPV = Vector3.Lerp(lastPV, Vector3.zero, curTime/smoothTime); 
				if (axes == RotationAxes.MouseXAndY)
				{
					transform.eulerAngles = new Vector3(transform.eulerAngles.x + lastPV.x , transform.eulerAngles.y + lastPV.y, 0);
				}
				else if (axes == RotationAxes.MouseX)
				{
					transform.eulerAngles = new Vector3(transform.eulerAngles.x , transform.eulerAngles.y + lastPV.y, 0);
				}
				else
				{
					transform.eulerAngles = new Vector3(transform.eulerAngles.x  + lastPV.x, transform.eulerAngles.y , 0);
				}									    
				curTime += Time.smoothDeltaTime;
			}
			//=====================
		}
		var xtemp = ClampX(transform.eulerAngles.x,Mathf.Sign(lastMouse.x));
		transform.eulerAngles = new Vector3(xtemp, transform.eulerAngles.y, transform.eulerAngles.z);
	}
	
	void Start ()
	{
		
		// Make the rigid body not change rotation
		if (rigidbody)
			rigidbody.freezeRotation = true;
		TranslateMinMaxX();
	}
	void TranslateMinMaxX()
	{
		if(minRotX < maxRotX)
		{			
			if(maxRotX > 0)
			{
				maxRotX = Mathf.Clamp(maxRotX, 0, 90);
				maxRotX = 360 - maxRotX;
				if(minRotX > 0)
				{
					minRotX = Mathf.Clamp(minRotX, 0, 90);
					minRotX = 360 - minRotX;
				}
				else if(minRotX < 0)
				{
					minRotX = Mathf.Clamp(minRotX, -90, 0);
					minRotX = - minRotX;
				}
			}
			else if(maxRotX < 0)
			{
				maxRotX = Mathf.Clamp(maxRotX, -90, 0);
				maxRotX = - maxRotX;
				minRotX = Mathf.Clamp(minRotX, -90, 0);
				minRotX = - minRotX;
			}			
		}
	}
	float ClampX(float xAngle , float sign )
	{
		float clampResult   = xAngle;
		if(xAngle > 359.5f)
		{
			if(sign > 0)
			{
				xAngle = 0;
			}
			else
			{
				xAngle = 359.5f;
			}				
		}			
		if(xAngle < 100)
		{
			return Mathf.Clamp(xAngle, 0, minRotX);
		}
		else
		{
			return  Mathf.Clamp(xAngle, maxRotX, 359.5f);
		}
	}
}
